/*
* jlb
*/
package com.jlb.basic.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.basic.entity.BasicArea;
import com.jlb.basic.service.BasicAreaSV;
import com.jlb.basic.vo.BasicAreaVO;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.tools.StringTools;
import com.jlb.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 行政区域 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/basicArea")
@Slf4j
@Api(value = "行政区域控制器", description = "行政区域控制器")
public class BasicAreaCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private BasicAreaSV basicAreaSV;

    /**
     * 区域管理
     */
    @ApiOperation(value = "区域管理", notes = "区域管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "省区域编码", required = true, paramType = "path")
    })
    @GetMapping("/{areaCode}")
    @ResponseBody
    public BeanRet loadAreaCode(@PathVariable String areaCode) {
        BasicArea basicArea = basicAreaSV.load(areaCode);
        return BeanRet.create(true, "", JSON.toJSONString(basicArea));
    }

    /**
     * 获得所有省
     */
    @ApiOperation(value = "区域管理", notes = "区域管理")
    @GetMapping("/provoices")
    @ResponseBody
    public BeanRet listProvoiceAll() {
        List<BasicArea> basicAreas = basicAreaSV.listProvoice();
        return BeanRet.create(true, "", JSON.toJSONString(basicAreas));
    }

    /**
     * 手动访问加载区域编号至redis缓存中
     *
     * @return
     */
    @ApiOperation(value = "加载区域编号至redis缓存中", notes = "加载区域编号至redis缓存中")
    @GetMapping("/queryAreas2Code")
    @ResponseBody
    public BeanRet queryAreas2Code() {
        boolean result = basicAreaSV.queryAll2Redis();
        return BeanRet.create(true, "成功", result);
    }

    /**
     * 根据区域编码区取区域信息;
     *
     * @param area_code
     * @return
     */

    @ApiOperation(value = "根据区域编码区取区域信息", notes = "根据区域编码区取区域信息")
    @GetMapping("/queryAreasByCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "level", value = "城市等级", paramType = "query", dataType = "Integer"),
    })
    @ResponseBody
    public BeanRet queryAreasByCode(String areaCode, Integer level) {
        BeanRet beanRet = null;
        if (level == null) level = 1;
        List<BasicArea> basicAreaList = basicAreaSV.queryAreasByCode(areaCode, level);
        if (basicAreaList != null && basicAreaList.size() > 0) {
            beanRet = BeanRet.create(true, "获取数据成功", basicAreaList);
        } else {
            beanRet = BeanRet.create(false, "无查询数据");
        }
        return beanRet;

    }

    ;


    /**
     * 根据area_code 查询对象信息及下级区域信息;
     *
     * @param area_code
     * @return
     */

    @ApiOperation(value = "加载区域编号至redis缓存中", notes = "加载区域编号至redis缓存中")
    @GetMapping("/getBasicAreaInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", dataType = "String"),
    })
    @ResponseBody
    public BeanRet getBasicAreaInfo(String areaCode) {
        BeanRet beanRet = null;
        Map<String, Object> map = new HashedMap();
        if (areaCode == null || areaCode.trim().equals(""))
            beanRet = BeanRet.create(false, "区域编码不能为空");
        map = basicAreaSV.getBasicAreaInfo(areaCode);
        if (!map.isEmpty() && map.size() > 0) {
            beanRet = BeanRet.create(true, "获取数据成功", map);
        } else {
            beanRet = BeanRet.create(false, "无查询数据");
        }
        return beanRet;

    }

    ;


    /**
     * 根据area_code 查询对象信息;
     *
     * @param area_code
     * @return
     */

    @ApiOperation(value = "加载区域编号至redis缓存中", notes = "加载区域编号至redis缓存中")
    @GetMapping("/getAreaByAreaCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "area_code", value = "区域编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaName", value = "区域名称", paramType = "query", dataType = "String"),
    })
    @ResponseBody
    public BeanRet getAreaByAreaCode(String areaCode, String areaName) {
        BeanRet beanRet = null;
        List<BasicArea> basicAreaList = basicAreaSV.getAreaByAreaCode(areaCode, areaName);
        if (!basicAreaList.isEmpty() && basicAreaList.size() > 0) {
            beanRet = BeanRet.create(true, "获取数据成功", basicAreaList);
        } else {
            beanRet = BeanRet.create(false, "无查询数据");
        }
        return beanRet;
    }

    ;


    /**
     * 根据区域编号修改区域信息（只能修改地区名称）
     *
     * @param areaCode 区域编码
     * @param areaName 区域名称
     */
    @ApiOperation(value = "修改区域信息", notes = "修改区域信息")
    @RequestMapping(value = "updateNameByCode", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区域编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaName", value = "区域名称", required = true, paramType = "query", dataType = "String")
    })
    @ResponseBody
    public BeanRet updateNameByCode(String areaCode, String areaName) {
        if (!StringTools.isNotEmpty(areaCode))
            return BeanRet.create(false, "区域编码为空");
        if (!StringTools.isNotEmpty(areaName))
            return BeanRet.create(false, "区域名称为空");

        basicAreaSV.updateNameByCode(areaCode, areaName);

        return BeanRet.create(true, "修改成功");
    }

    /**
     * 添加区域信息
     *
     * @param level    区域级别
     * @param areaCode 区域编码
     * @param areaName 区域名称
     * @return
     */
    @ApiOperation(value = "添加区域信息", notes = "添加区域信息")
    @RequestMapping(value = "addArea", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区域编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "level", value = "区域级别", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "areaName", value = "区域名称", required = true, paramType = "query", dataType = "String")
    })
    @ResponseBody
    public BeanRet addArea(Integer level, String areaCode, String areaName) {
        if (level == null)
            return BeanRet.create(false, "区域级别为空");
        if (!StringTools.isNotEmpty(areaCode))
            return BeanRet.create(false, "区域编码为空");
        if (!StringTools.isNotEmpty(areaName))
            return BeanRet.create(false, "区域名称为空");
        //判断区域是否已存在
        boolean flag = basicAreaSV.isExistArea(areaCode, areaName, level);
        if (flag) return BeanRet.create(false, "区域已存在");
        basicAreaSV.addArea(level, areaCode, areaName);

        return BeanRet.create(true, "添加成功");

    }

    /**
     * 手动生成区域前2级json串
     * 192.168.10.110:8088/res/area/queryAreaLevelSecond
     *
     * @return
     */
    @ApiOperation(value = "手动生成指定级别区域前的所有json串", notes = "手动生成区域前2级json串")
    @RequestMapping(value = "queryAreaLevel", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "区域级别", required = true, paramType = "query", dataType = "Integer"),
    })
    @ResponseBody
    public BeanRet queryAreaLevel(int level) {

        long start = System.currentTimeMillis();
        List<BasicAreaVO> data = basicAreaSV.queryAreasLevel(level);

        long end = System.currentTimeMillis();
        System.out.println(level + "级区域查询时间 = " + (end - start) / 1000 + "s");
//        System.out.println("JSON.toJSONString(data) = " + JSON.toJSONString(data));

        return BeanRet.create(true, "生成成功", data);
    }


}
